package com.example.galerija;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.database.DatabaseHandler;
import com.example.database.Group;

public class GroupDetailFragment extends Fragment{
	   /**
     * The fragment argument representing the item ID that this fragment
     * represents.
     */
    public static final String ARG_ITEM_ID = "item_id";

    /**
     * The person this fragment is presenting.
     */
    private Group mGroup;
    
    /**
     * The UI elements showing the details of the Person
     */
    
    private TextView textCategory_code;
    private TextView textDescription;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public GroupDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments().containsKey(ARG_ITEM_ID)) {
        	// Should use the contentprovider here ideally
            mGroup = DatabaseHandler.getInstance(getActivity()).getGroup(getArguments().getLong(ARG_ITEM_ID));
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_group_detail, container, false);

        if (mGroup != null) {
            textCategory_code = ((TextView) rootView.findViewById(R.id.textCategory_code));
            textCategory_code.setText(mGroup.item_category_code);
            
            textDescription = ((TextView) rootView.findViewById(R.id.textDescription));
            textDescription.setText(mGroup.description_lv);
            
        }

        return rootView;
    }
    
    @Override
    public void onPause() {
    	super.onPause();
    	updatePersonFromUI();
    }
    
    private void updatePersonFromUI() {
    	if (mGroup != null) {
    		mGroup.item_category_code = textCategory_code.getText().toString();
    		mGroup.description_lv = textDescription.getText().toString();
    	
    		
    		DatabaseHandler.getInstance(getActivity()).putGroup(mGroup);
        }
    }

}
